

function test (nums, target) {
  const obj = {};
  const result = [];

  nums.forEach((num, index) => {
    if (num > target) return;

    if (obj[num]) {
      obj[num].push(index);
    } else {
      obj[num] = [index];
    }
  });

  console.log(obj);
  for (const key in obj) {
    if ((target - key) === key) {
      if (obj[key] && obj[key].length > 2) {
        return obj[key];
      } else {
        return [];
      }
    }

    if (obj[target - key] && obj[target - key].length > 0) {
      result.push(obj[key].shift());
      result.push(obj[target - key].shift());

      return result;
    }
  }
}

const result = test([1, 2, 3, 4, 5, 6, 7, 2, 3 ,4 ,5], 2);
console.log(result);